package NGLR;


import GameLogic.Background;
import Renderer.BackgroundImage;
import org.lwjgl.BufferUtils;
import utilities.Image;
import utilities.TextureDecoder;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL12.*;
import static org.lwjgl.opengl.ARBTextureRectangle.*;

import java.nio.IntBuffer;

class BackgroundRendering {

    private Image bgi;
    private double image_aspect_ratio;
    private Background bg;
    private IntBuffer texture_handle;


    public BackgroundRendering(Background bg){
        this.bg = bg;
        bgi = TextureDecoder.LoadImage(bg.TexturePath);
        image_aspect_ratio = bgi.width / bgi.height;
        texture_handle = BufferUtils.createIntBuffer(1);
        glGenTextures(texture_handle);
        glBindTexture(GL_TEXTURE_RECTANGLE_ARB, texture_handle.get(0));
        glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
        glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, bgi.width, bgi.height,0, bgi.format, GL_UNSIGNED_BYTE,bgi.data);
        glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);
    }

    public double getZ(){
        return bg.MapZPos;
    }

    public void Draw(double Offset, double aspect_ratio, double scaller){
        double scale =  (bg.AbsoluteCoordinates ? scaller * aspect_ratio : 1.0);
        double range = 0.5;
        double img_lenght = scaller * image_aspect_ratio;
        double scale_factor = (1.0/ ((bg.MapYPosBeg < 0 ? -bg.MapYPosBeg : bg.MapYPosBeg) + (bg.MapYPosEnd < 0 ? -bg.MapYPosEnd : bg.MapYPosEnd))) * scale;


            for(int i = 0; bg.BackgroundLoops == 0 || i < bg.BackgroundLoops; i++){
                //System.out.println(">>>>>>   " + (Offset +(aspect_ratio + img_lenght + range) * (i)) + " > " + bg.MapOffset);
                //System.out.println("<<<<<<   " + Offset + " > " + (bg.MapOffset - (aspect_ratio + img_lenght) * i));
                //if (!(Offset +(aspect_ratio + img_lenght + range) * (i) > bg.MapOffset  )) {
             //   if((bg.MapOffset + (img_lenght * (i + 1)) < Offset - aspect_ratio)){
             //       System.out.println("1 IF; i=" + i + "    ||   " + (bg.MapOffset + (img_lenght * i) + aspect_ratio) + " > " + Offset);
              //      continue;
              //  }
               // if (!(Offset > bg.MapOffset - (aspect_ratio + img_lenght) * i)){
              //  if((bg.MapOffset + (img_lenght * i) > Offset + aspect_ratio)){
              //       System.out.println("2 IF; i=" +i + "    ||   " + (bg.MapOffset + img_lenght *  i) + " < " + (Offset + aspect_ratio));
              //     break;
              //  }

                //TODO: It renders ALL pictures, even not seen !!!!

               glEnable(GL_TEXTURE_RECTANGLE_ARB);
               glBindTexture(GL_TEXTURE_RECTANGLE_ARB, texture_handle.get(0));

                glPushMatrix();
                //System.out.println("TRANS: "+ i + ": " + (bg.MapOffset + image_aspect_ratio * i * scale - Offset * bg.BackgroundSpeed / scale  + (image_aspect_ratio/2.0)));
                glTranslated(bg.MapOffset + image_aspect_ratio * i * scale - Offset * bg.BackgroundSpeed / scale  + (image_aspect_ratio/2.0), (bg.MapYPosBeg + bg.MapYPosEnd)/2.0, bg.MapZPos);
                glScaled(-scale_factor * image_aspect_ratio, scale_factor, 1.0);
                glBegin(GL_QUADS);
                    glTexCoord2f(bgi.width, bgi.height);
                    glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
                    glVertex2f(-1.0f, -1.0f);
                    glTexCoord2f(0.0f, bgi.height);
                    glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
                    glVertex2f(1.0f, -1.0f);
                    glTexCoord2f(0.0f, 0.0f);
                    glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
                    glVertex2f(1.0f, 1.0f);
                    glTexCoord2f(bgi.width, 0.0f);
                    glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
                    glVertex2f(-1.0f, 1.0f);

                glEnd();
                glDisable(GL_TEXTURE_RECTANGLE_ARB);
                glPopMatrix();
            }
       }
    //}

    public  void Clean(){
        glDeleteTextures(texture_handle);
    }
}